package com.books.controller.login_regis;/*
 *@Auther Zhourui
 *@data 2021/9/26  20:46
 *@Email 1605576647@qq.com
 */

import com.books.model.bo.Result;
import com.books.model.entity.common.People;
import com.books.model.entity.login_regis.UserLogin;
import com.books.service.login_regis.IUserLoginService;
import com.books.service.people.IPeopleService;
import com.books.util.Md5Util;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

@Controller
@RequestMapping(value = "/user",method= RequestMethod.GET)
public class UserLoginController {
    @Autowired(required = false)
    private IUserLoginService userLoginService;

    @Autowired(required = false)
    private IPeopleService peopleService;

    @Autowired(required = false)
    private People people;

    @PostMapping("/loginuser")
    @ResponseBody
    public Result loginuser(HttpServletRequest request,String account, String passwd){
        HttpSession session=request.getSession();
        Integer statusId;
        people=peopleService.getPeopleByAccount(account);
        if(people==null){
            statusId=1;
        }
        else {
            statusId=people.getStatusId();
        }
        UserLogin u1=userLoginService.loginByUname(account);
        UserLogin u2=userLoginService.loginByAccount(account);
        UserLogin u3=userLoginService.loginByEmail(account);
        if(statusId==2){
            return new Result("500","该号已被冻结！");
        }
        else if(statusId==3){
            return new Result("500","该账户已做封号处理！");
        }
        else if(u1==null&&u2==null&&u3==null) {
            return new Result("404", "用户名不存在");
        }
        else if(u1!=null){
            if(u1.getPassword().equals(Md5Util.code(passwd))){
                people=peopleService.getPeopleByAccount(u1.getAccount());
                session.setAttribute("people",people);
                session.setAttribute("user",u1);
            return new Result("200","登录成功");
             }
        }
        else if(u2!=null){
            if(u2.getPassword().equals(Md5Util.code(passwd))){
                people=peopleService.getPeopleByAccount(u2.getAccount());
                session.setAttribute("people",people);
                session.setAttribute("user",u2);
                return new Result("200","登录成功");
            }
        }
        else if(u3!=null){
            if(u3.getPassword().equals(Md5Util.code(passwd))){
                people=peopleService.getPeopleByAccount(u3.getAccount());
                session.setAttribute("people",people);
                session.setAttribute("user",u3);
                return new Result("200","登录成功");
            }
        }
        return new Result("500","密码错误");
    }
}
